package 函数式接口;

import org.junit.Test;

import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * Description
 *
 * @Author: SunAo
 * @Date: 2021/2/25 16:43
 */
public class 方法引用 {

    @Test
    public void 对象的方法引用(){
        String message = "Hello";
        // boolean equals(Object anObject)  -> String().equals
        // boolean test(T t)                -> Predicate.test
        // equals 方法的出入参和Predicate的函数接口方法一样，所以可以用方法引用替代
        Predicate<String> predicate1 = t -> message.equals(t);
        Predicate<String> predicate2 = message::equals;

        System.out.println(predicate1.test("Hello"));
        System.out.println(predicate2.test("Hello"));
    }

    @Test
    public void 静态方法引用(){

        // void accept(T t)        -> Consumer.accept
        // void println(String x)  -> println
        Consumer<String> con1 = s -> System.out.println(s);
        con1.accept("Hello World");

        Consumer<String> con2 = System.out::println;
        con2.accept("Hello World");
    }

    @Test
    public void 类实例方法(){

        // boolean test(T t, U u);          -> BiPredicate.test
        // boolean equals(Object anObject)  -> String.equals
        BiPredicate<String,String> predicate1 = (a,b)->a.equals(b);
        System.out.println(predicate1.test("Hello","Hello"));

        BiPredicate<String,String> predicate2 = String::equals;
        System.out.println(predicate2.test("Hello","Hello"));

    }
}
